package com.study.demo.springmvc.validator;

import com.study.demo.springmvc.model.User;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * @author: elvin
 * @time: 2019-05-08 10:53
 * @desc:
 **/
@Component
public class UserValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return User.class.equals(clazz);
    }

    @Override
    public void validate(Object obj, Errors errors) {
        User user = (User) obj;
        if(user.getName().length() < 8){
            Object[] errorArgs = {"minLength", 8};
            errors.rejectValue("name", "valid.name.length", errorArgs, "name's length can not less than "+errorArgs[1]+" place ;");
        }
    }
}
